import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { SERVER_API_URL } from '../../app.constants';

import { JhiDateUtils } from 'ng-jhipster';

import { BookBasic } from './book-basic.model';
import { createRequestOption } from '../../shared';
import {BookType} from "../book-type/book-type.model";

export type EntityResponseType = HttpResponse<BookBasic>;

@Injectable()
export class BookBasicService {

    private resourceUrl =  SERVER_API_URL + 'api/book-basics';

    constructor(private http: HttpClient, private dateUtils: JhiDateUtils) { }

    create(bookBasic: BookBasic): Observable<EntityResponseType> {
        const copy = this.convert(bookBasic);
        return this.http.post<BookBasic>(this.resourceUrl, copy, { observe: 'response' })
            .map((res: EntityResponseType) => this.convertResponse(res));
    }

    update(bookBasic: BookBasic): Observable<EntityResponseType> {
        const copy = this.convert(bookBasic);
        return this.http.put<BookBasic>(this.resourceUrl, copy, { observe: 'response' })
            .map((res: EntityResponseType) => this.convertResponse(res));
    }

    find(id: number): Observable<EntityResponseType> {
        return this.http.get<BookBasic>(`${this.resourceUrl}/${id}`, { observe: 'response'})
            .map((res: EntityResponseType) => this.convertResponse(res));
    }

    query(req?: any): Observable<HttpResponse<BookBasic[]>> {
        const options = createRequestOption(req);
        return this.http.get<BookBasic[]>(this.resourceUrl, { params: options, observe: 'response' })
            .map((res: HttpResponse<BookBasic[]>) => this.convertArrayResponse(res));
    }


    delete(id: number): Observable<HttpResponse<any>> {
        return this.http.delete<any>(`${this.resourceUrl}/${id}`, { observe: 'response'});
    }

    private convertResponse(res: EntityResponseType): EntityResponseType {
        const body: BookBasic = this.convertItemFromServer(res.body);
        return res.clone({body});
    }

    private convertArrayResponse(res: HttpResponse<BookBasic[]>): HttpResponse<BookBasic[]> {
        const jsonResponse: BookBasic[] = res.body;
        const body: BookBasic[] = [];
        for (let i = 0; i < jsonResponse.length; i++) {
            body.push(this.convertItemFromServer(jsonResponse[i]));
        }
        return res.clone({body});
    }

    public getAllBooks = (page: number, size: number): Observable<BookBasic[]> => {
        return this.http.get<BookBasic[]>(this.resourceUrl + '?page='+ page + '&size=' + size);
    }

    public findBooksByType = (id: number ,page: number, size: number): Observable<any> => {
        return this.http.get<any>(this.resourceUrl + '/bookType?bookType='+ id + '&page='+ page + '&size=' + size);
    }
    public findBooksByKeyword = (type: number , keyword: string): Observable<any> => {
        return this.http.get<any>(this.resourceUrl + '/keyword?keyword=' + encodeURIComponent(keyword) + '&type=' + type );
    }
    /**
     * Convert a returned JSON object to BookBasic.
     */
    private convertItemFromServer(bookBasic: BookBasic): BookBasic {
        const copy: BookBasic = Object.assign({}, bookBasic);
        copy.pubDate = this.dateUtils
            .convertDateTimeFromServer(bookBasic.pubDate);
        return copy;
    }

    /**
     * Convert a BookBasic to a JSON which can be sent to the server.
     */
    private convert(bookBasic: BookBasic): BookBasic {
        const copy: BookBasic = Object.assign({}, bookBasic);

        copy.pubDate = this.dateUtils.toDate(bookBasic.pubDate);
        return copy;
    }
}
